package com.guigu.guliauth.controller;

import com.guigu.guliauth.service.LoginService;
import com.guigu.gulicommon.entity.pojo.res.Res;
import com.guigu.gulicommon.entity.vo.UserLoginVo;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;

/**
 * 登陆相关接口
 *
 * @author 陈俊杰
 * @since 2024-8-4
 */
@RestController
@RequestMapping(value = "/login")
@Log4j2
@Validated
public class LoginController {
    @Autowired
    private LoginService loginService;

    /**
     * 根据用户名和密码登录
     *
     * @param userName 用户名
     * @param password 密码
     * @return 登录信息
     */
    @GetMapping(value = "/password-and-username")
    public Res<UserLoginVo> loginByPasswordAndUsername(
            @NotBlank(message = "登陆账号不能为空")
            String userName,
            @NotBlank(message = "登陆密码不能为空")
            String password
    ) {
        log.info("根据用户名和密码登录:userName={},password={}", userName, password);
        return loginService.loginByPasswordAndUsername(userName, password);
    }

    /**
     * 使用 Gitee 登陆
     *
     * @param code gitee三方登录用户授权码
     * @return 登录信息
     */
    @GetMapping(value = "/gitee")
    public Res<UserLoginVo> loginByGitee(@NotBlank(message = "授权码不能为空") String code) {
        log.info("gitee授权码:{}", code);
        return loginService.loginByGitee(code);
    }
}
